import CryptoJS from 'crypto-js'
import { SECRET } from './constant'

// 使用自定义密码加密密码
export function encryptPassword(password: string): string {
  return CryptoJS.AES.encrypt(password, SECRET).toString()
}

// 使用输入的密码解密密文
export function decryptPassword(cipherText: string, secret: string): string {
  try {
    const bytes = CryptoJS.AES.decrypt(cipherText, secret)
    const original = bytes.toString(CryptoJS.enc.Utf8)
    if (!original) throw new Error('Wrong secret key')
    return original
  } catch {
    return '❌ 解密失败，口令错误'
  }
}
